package cn.btks.resultintegration.catalog.controller;

import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.valid.SystemLog;
import cn.btks.resultintegration.achievements.dto.AchievementQueryParamDto;
import cn.btks.resultintegration.achievements.pojo.Achievements;
import cn.btks.resultintegration.achievements.service.AchievementsService;
import cn.btks.resultintegration.achievements.service.IServerService;
import cn.btks.resultintegration.catalog.dto.CatalogDto;
import cn.btks.resultintegration.catalog.pojo.PolicyFile;
import cn.btks.resultintegration.catalog.service.CatalogService;
import cn.btks.resultintegration.catalog.service.PolicyFileService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author lijun
 * @Date 2020/12/16 17:05
 */
@Slf4j
@RestController
@RequestMapping("api/v1.0/resultIntegration/policyFile")
@Api(tags = "政策文件模块")
@Validated
public class PolicyFileController {
    
    @Autowired
    PolicyFileService policyFileService;

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询政策文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "文件ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id") Long id) {
        PolicyFile file = policyFileService.findById(id);
        return file != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), file) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findByName/{fileName}")
    @ApiOperation("根据目录结构ID查询政策文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "fileName", value = "文件名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findByName(@PathVariable("fileName") String fileName) {
        List<PolicyFile> list = policyFileService.findByName(fileName);
        return list != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), list) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findByCatalogId/{catalogId}")
    @ApiOperation("根据目录结构ID查询政策文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "catalogId", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findByCatalogId(@PathVariable("catalogId") Long catalogId) {
        List<PolicyFile> list = policyFileService.findByCatalogId(catalogId);
        return list != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), list) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @SystemLog(type = SystemLogEnum.SAVE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @PostMapping(value = "/save",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "新增成果数据", notes = "新增成果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "filePath", value = "文件路径", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "fileTye", value = "文件类型", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogId", value = "所在目录ID", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(PolicyFile policyFile, @ApiParam(name = "多文件", value = "multipartFile", required = true) MultipartHttpServletRequest multipartFiles, HttpServletRequest request) throws Exception {
        MultiValueMap<String, MultipartFile> map = multipartFiles.getMultiFileMap();
        List<MultipartFile> list = map.get("multipartFile");// 获取到文件的列表
        return policyFileService.save(policyFile,list,request);
    }

    @SystemLog(type = SystemLogEnum.DELETE_LOG,moduleMessage = MenuSourceEnum.MENU_RESULT)
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除政策文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "成果ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState delete(@PathVariable("id") Long id) {
        return policyFileService.delete(id);
    }
}
